package com.yanqu.road.server.cmd.banquet.crossBanquet.auto;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.servercenter.TitleInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.config.TitleMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_BANQUET_CROSS_ACTIVITY_JOIN_AUTO_SET, desc = "自动赴宴设置")
public class BanquetAutoJoinSetCmd extends AbstractCommand {

	private CrossBanquetProto.BanquetAutoJoinSetReq reqMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = CrossBanquetProto.BanquetAutoJoinSetReq.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo crbAcInfo = NormalActivityMgr.getActivityInfo(reqMsg.getAcId());
		//必须是跨服争霸，就是三个区服的才能开宴会
		if(crbAcInfo == null || crbAcInfo.getServerIdList().size() <= 2){
			ret = GameErrorCode.E_BANQUET_CROSS_RANK_ACTIVITY_OVER;
			return;
		}
		if(!ActivityMgr.activityInTime(crbAcInfo)){
			ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
			return;
		}
		//现在只有跨服宴会争霸的两个宴会会自动回宴的类型是50001和50002 都是礼物类型type1是5的
		Property allReward = new Property();
		for(CrossBanquetProto.BanquetAutoJoinTmp joinTemp : reqMsg.getJoinsList()){
			if(!BanquetConfigMgr.isBanquetConsumeContain(5,joinTemp.getConsume())){
				ret = GameErrorCode.E_BANQUET_GIFT_ERROR;
				return;
			}
			Property giftConsume = PropertyHelper.parseStringToProperty(joinTemp.getConsume());
			giftConsume.goodCountMultiNum(joinTemp.getNum());
			allReward.addProperty(giftConsume);
		}
		if (!player.getModule(CurrencyModule.class).currencyIsEnough(allReward)) {
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		// 预扣除一下
		if (!StringUtils.isNullOrEmpty(PropertyHelper.parsePropertyToString(allReward))) {
			player.getModule(PreReduceModule.class).preReduce(allReward);
		}

		CrossBanquetProto.CrossBanquetAutoJoinSetReq.Builder reqCrossMsg = CrossBanquetProto.CrossBanquetAutoJoinSetReq.newBuilder();
		reqCrossMsg.setAcId(reqMsg.getAcId());
		reqCrossMsg.addAllJoins(reqMsg.getJoinsList());
		reqCrossMsg.setConsume(PropertyHelper.parsePropertyToString(allReward));
		TitleInfo titleInfo = TitleMgr.getTitleInfo(player.getTitleId());
		//头衔加成
		int titleAdd = titleInfo.getBanquetEffect();
		//门客技能加成
		int skillAdd = BanquetMgr.getBanquetJoinSkillAddition(player);
		reqCrossMsg.setTitleAdd(titleAdd);
		reqCrossMsg.setSkillAdd(skillAdd);
		reqCrossMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_BANQUET_JOIN_AUTO_SET, reqCrossMsg);
		GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			CrossBanquetProto.BanquetAutoJoinSetResp.Builder respMsg = CrossBanquetProto.BanquetAutoJoinSetResp.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_BANQUET_CROSS_ACTIVITY_JOIN_AUTO_SET, respMsg);
		}
	}
}
